package kuzmenkov.oip.controller;

import kuzmenkov.oip.service.ModuleChoosingService;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.springframework.web.jsf.FacesContextUtils;

import javax.faces.context.FacesContext;
import java.util.logging.Logger;

/**
 * Class is responsible for interacting with user. It helps to choose a module.
 * It is a backing bean for a module choosing page
 * User: Michael Kuzmenkov
 * Date: 18.03.2009
 * Time: 21:36:13
 */
public class ModuleChoosingController extends ErrorSupport {
    private static final Logger log = Logger.getLogger("global");

    private ModuleChoosingService service;
    private HtmlTree tree;


    public ModuleChoosingController() {
        try {
            service = (ModuleChoosingService) FacesContextUtils.
                    getWebApplicationContext(FacesContext.getCurrentInstance()).
                    getBean("moduleChoosingService");
        } catch (Exception e) {
            addError(e);
        }
    }

    public HtmlTree getTree() {
        return tree;
    }

    public void setTree(HtmlTree tree) {
        this.tree = tree;
    }

    public String getModuleProducerName() {
        String name = null;
        try {
            name = service.getModuleProducerName();
        } catch (Exception e) {
            addError(e);
        }
        return name;
    }

    public String getElementName() throws Exception {
        String name = null;
        try {
            if (service.isModuleSelection())
                name = service.getModuleName();
            else if (service.isClassSelection())
                name = service.getClassName();
        } catch (Exception e) {
            addError(e);
        }

        return name;
    }

    public String getElementDescription() {
        String desc = null;
        try {
            if (service.isModuleSelection())
                desc = service.getModuleDescription();
            else if (service.isClassSelection())
                desc = service.getClassDescription();
        } catch (Exception e) {
            addError(e);
        }
        return desc;
    }

    public TreeNode<String> getRootNode() {
        TreeNode<String> node = null;
        try {
            node = service.getRootNode();
        } catch (Exception e) {
            addError(e);
        }
        return node;
    }


    /**
     * Processes an event of the tree element selection
     *
     * @param event
     */
    public void processSelection(NodeSelectedEvent event) {
        try {
            HtmlTree tree = (HtmlTree) event.getComponent();
            String selectedElement = tree.getRowKey().toString();
            log.info("selectedElement: " + selectedElement);

            String[] keys = selectedElement.split(":");
            String key = keys[keys.length - 1];
            boolean isModule = false;
            if (key.startsWith("m"))
                isModule = true;
            else if (key.startsWith("c"))
                isModule = false;
            else
                log.warning("Cannot define a type of the selected tree element");

            Long id = Long.parseLong(key.substring(1));
            if (isModule)
                processModuleSelection(id);
            else
                processClassSelection(id);
        } catch (Exception e) {
            addError(e);
        }
    }

    private void processClassSelection(Long id) {
        try {
            service.setCurrentClass(id);
        } catch (Exception e) {
            addError(e);
        }
    }

    private void processModuleSelection(Long id) {
        try {
            service.setCurrentModule(id);
        } catch (Exception e) {
            addError(e);
        }
    }

    /**
     * @return true if a module or a method class is selected
     */
    public boolean isSelectedElement() {
        boolean f = false;
        try {
            f = service.isModuleSelection() || service.isClassSelection();
        } catch (Exception e) {
            addError(e);
        }
        return f;
    }

    /**
     * @return true if a module is selected
     */
    public boolean isSelectedModule() {
        boolean f = false;
        try {
            f = service.isModuleSelection();
        } catch (Exception e) {
            addError(e);
        }
        return f;
    }

}
